package com.bafa.front.events;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bafa.dbcache.LeagueCache;
import com.bafa.debug.Debug;
import com.bafa.front.presentationbeans.LeagueBean;
import com.bafa.front.presentationbeans.LeaguePositionBean;
import com.bafa.front.presentationbeans.PlayerListBean;
import com.bafa.front.presentationbeans.TeamBean;
import com.bafa.front.utils.FrontUtils;
import com.bafa.objects.PageBean;
import com.bafa.services.RetrieveServices;

public class UserHomeEvent extends Event {

	@Override
	public void doEvent(HttpServletRequest request,
			HttpServletResponse response, PageBean page) {
		Debug.debug(this.getClass(),"doEvent","Entering...");
		LeagueBean league = (LeagueBean)request.getSession().getAttribute("DIVISION");
		Debug.debug(this.getClass(),"doEvent","League "+league);
		Debug.debug(this.getClass(),"doEvent","User "+FrontUtils.getUser(request));
		Debug.debug(this.getClass(),"doEvent","Team Id "+FrontUtils.getUser(request).getTeamId());
		Debug.debug(this.getClass(),"doEvent","Team Object "+RetrieveServices.getTeam(FrontUtils.getUser(request).getTeamId()+""));
		Debug.debug(this.getClass(),"doEvent","Season "+FrontUtils.getThisSeason(request).getId());
		TeamBean team = new TeamBean(RetrieveServices.getTeam(FrontUtils.getUser(request).getTeamId()+""),FrontUtils.getThisSeason(request).getId());
		Debug.debug(this.getClass(),"doEvent","Team "+team);
		LeaguePositionBean allTimeRecord = new LeaguePositionBean(FrontUtils.getUser(request).getTeamId());
		LeaguePositionBean leagueRecord = new LeaguePositionBean(FrontUtils.getUser(request).getTeamId(),FrontUtils.getThisSeason(request).getId());
		PlayerListBean allTimeScorers = new PlayerListBean(RetrieveServices.getTeam(FrontUtils.getUser(request).getTeamId()+"").getStats().scorers(),team.getId());
		request.setAttribute("SCORERS",allTimeScorers);
		request.setAttribute("RECORD",allTimeRecord);
		request.setAttribute("LEAGUERECORD",leagueRecord);
		request.setAttribute("TEAM",team);
		request.getSession().setAttribute("LOGGEDONTEAM",team);
		page.addDetails(RetrieveServices.getPage("user"));
		Debug.debug(this.getClass(),"doEvent","1. Page ID is: "+page.getId());
		page.setPageheader(page.getPageheader().replaceAll("<>", team.getName()));
        page.setMainheader(page.getMainheader().replaceAll("<>", team.getName()));
		Debug.debug(this.getClass(),"doEvent","2. Page ID is: "+page.getId());
		Debug.debug(this.getClass(),"doEvent","....Leaving");
	}

}
